package com.syq.web;

import com.syq.bean.Grade;
import com.syq.bean.Role;
import com.syq.bean.Student;
import com.syq.bean.Users;
import com.syq.service.GradeService;
import com.syq.service.RoleService;
import com.syq.service.StudentService;
import com.syq.service.UsersService;
import com.syq.service.impl.GradeServiceImpl;
import com.syq.service.impl.RoleServiceImpl;
import com.syq.service.impl.StudentServiceImpl;
import com.syq.service.impl.UsersServiceImpl;
import com.syq.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/user/users")
public class UserServlet extends HttpServlet {
    UsersService usersService = new UsersServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)) {
            select(req,resp);
        }else if("adduser".equals(method)) {
            addUser(req,resp);
        }else if("updateuser".equals(method)) {
            updateUser(req,resp);
        }else if("findbyid".equals(method)) {
            findById(req,resp);
        } else if("deleteuser".equals(method)) {
            deleteUser(req,resp);
        } else if("getRoleList".equals(method)){
            RoleService roleService = new RoleServiceImpl();
            List<Role> roles = roleService.getList();
            req.setAttribute("rlist",roles);
            req.getRequestDispatcher("add.jsp").forward(req,resp);
        }else if ("info".equals(method)) {
            getInfo(req,resp);
        }
    }
    protected void getInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        UsersService service = new UsersServiceImpl();
        Users user = service.findById(Integer.parseInt(userId));
        //查询列表
        RoleService roleService = new RoleServiceImpl();
        List<Role> list = roleService.getList();
        req.setAttribute("rlist",list);
        req.setAttribute("user",user);
        System.out.println("选号："+ user.getUserId());
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }

        protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        //System.out.println(userId);
        UsersService service = new UsersServiceImpl();
        int i = service.delUser(userId);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/user/users?method=select'</script>");
        }
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        UsersService service = new UsersServiceImpl();
        Users user = service.findById(Integer.parseInt(userId));
        //查询年纪列表
        RoleService roleService = new RoleServiceImpl();
        List<Role> list = roleService.getList();
        req.setAttribute("rlist",list);
        req.setAttribute("user",user);
        System.out.println("选号："+ user.getUserId());
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    //修改用户
    protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        String roleId = req.getParameter("roleId");
        //将参数封装到学生对象中
        Users user = new Users();
        user.setUserId(Integer.parseInt(userId));
        user.setLoginName(loginName);
        user.setPassWord(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setAddress(address);
        user.setEmail(email);
        user.setCardId(cardId);
        user.setDesc(desc);
        user.setPhone(phone);
        user.setRoleId(Integer.parseInt(roleId));
        //调取service
        UsersService usersService = new UsersServiceImpl();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        int i = usersService.updateUser(user);
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/power/user/users?method=findbyid&userId="+userId+"'</script>");
        }
    }
    //添加用户
    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        String roleId = req.getParameter("roleId");
        //将参数封装到学生对象中
        Users user = new Users();
        user.setLoginName(loginName);
        user.setPassWord(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setAddress(address);
        user.setEmail(email);
        user.setCardId(cardId);
        user.setDesc(desc);
        user.setPhone(phone);
        user.setRoleId(Integer.parseInt(roleId));
        //调取service
        UsersService usersService = new UsersServiceImpl();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        int i = usersService.insertUser(user);
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/power/user/users?method=select'</script>");
        }
    }
    //查询数据 （分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String index = req.getParameter("index");
        System.out.println("intdex:"+index);
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //查询数据列表
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageIndex(pageIndex);
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        System.out.println(usersList.get(0).getUserId());
        //总条数
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setDataList(usersList);
        //重定向
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
